import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'common/index.dart';

void main() async {
  // 确保Flutter绑定初始化
  WidgetsFlutterBinding.ensureInitialized();

  // 安装日志过滤器
  LogFilter.install();

  // 过滤掉ImageReader_JNI的警告日志
  // 暂时禁用以避免错误处理器导致的问题
  // _filterImageReaderWarnings();

  // 设置状态栏为白色（与页面背景一致）
  // SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
  //   statusBarColor: Colors.white, // 状态栏背景设为白色
  //   statusBarIconBrightness: Brightness.dark, // 状态栏图标为深色
  //   statusBarBrightness: Brightness.light, // iOS状态栏亮度
  // ));

  // 初始化全局服务
  await Global.init();

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    // 使用 ScreenUtilInit 包裹整个应用
    return ScreenUtilInit(
      // 设计稿的尺寸（单位是 px）
      designSize: const Size(375, 812), // iPhone X 的设计尺寸
      minTextAdapt: true, // 是否根据屏幕宽度自适应文本
      splitScreenMode: true, // 支持分屏模式
      builder: (context, child) {
        return GetMaterialApp(
          title: '玩真的',
          debugShowCheckedModeBanner: false, // 去掉右上角DEBUG标签
          theme: ThemeData(
            // colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            useMaterial3: true,
          ),
          darkTheme: ThemeData(
            brightness: Brightness.dark,
            colorScheme: ColorScheme.fromSeed(
              seedColor: Colors.deepPurple,
              brightness: Brightness.dark,
            ),
            useMaterial3: true,
          ),
          themeMode: Get.find<ThemeService>().themeMode,
          // 路由
          initialRoute: RouteNames.systemSplash, // 设置为闪屏页
          // initialRoute: RouteNames.systemLogin, // 设置为登录页
          getPages: RoutePages.list,
          defaultTransition: Transition.fadeIn,
          locale: const Locale('zh', 'CN'),
          fallbackLocale: const Locale('en', 'US'),
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: const [Locale('zh', 'CN'), Locale('en', 'US')],
          // 添加路由观察者
          navigatorObservers: [
            RoutePages.observer, // 使用RoutePages中定义的观察者
          ],
          // 路由中间件
          routingCallback: (routing) {
            if (routing?.current != null) {
              debugPrint('路由中间件: ${routing!.current}');
              // 记录初始路由
              if (RoutePages.history.isEmpty && routing.current != '/') {
                RoutePages.addHistory(routing.current);
              }
            }
          },
        );
      },
    );
  }
}
